package com.fram.controller.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.api.circle.request.CircleAddRequest;
import com.fram.bean.api.circle.request.CircleOperateRequest;
import com.fram.bean.api.circle.request.CircleSeachRequest;
import com.fram.bean.api.news.request.CommentSearchRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.NewsComment;
import com.fram.entity.ResourceCircle;
import com.fram.service.api.v1.ResourceCircleService;

/** 生活圈 **/
@RestController
@RequestMapping("/api/{version}/circle")
public class CircleApiController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(CircleApiController.class);

	@Autowired
	private ResourceCircleService resourceCircleService;

	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<CircleSeachRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getParam() == null) {
				request.setParam(new CircleSeachRequest());
			}
			try {
				request.getParam().setLoginUserId(getApiUserId());
				if (request.getParam().getIsMy()) {
					request.getParam().setUserId(request.getParam().getLoginUserId());
				}
			} catch (ProException e) {
				if (request.getParam().getIsMy()) {
					throw new ProValiDataException(e.getErrorCode(), e.getErrorMsg());
				}
			}
			msg.setData(resourceCircleService.list(request));
		} catch (ProException e) {
			LOG.error("-动态圈-种植圈展示出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-动态圈-种植圈展示异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "auth/add", method = RequestMethod.POST)
	private ResponseMsg add(@RequestBody CircleAddRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			resourceCircleService.add(request);
		} catch (ProException e) {
			LOG.error("-动态圈-发布种植圈出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-动态圈-发布种植圈异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 删掉等于修改 emptyDelete **/
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/update", method = RequestMethod.POST)
	private ResponseMsg update(@RequestBody ResourceCircle request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			resourceCircleService.update(request);
		} catch (ProException e) {
			LOG.error("-动态圈-删除动态->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-动态圈-删除动态->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 点赞 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "auth/operate", method = RequestMethod.POST)
	private ResponseMsg operate(@RequestBody CircleOperateRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			msg.setReturnMsg(resourceCircleService.operateCircle(request));
		} catch (ProException e) {
			LOG.error("-动态圈-动态操作->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-动态圈-动态操作->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 发布评论 */
	@ApiVersion(1.0)
	@RequestMapping("auth/comment/add")
	public ResponseMsg newcomment(@RequestBody NewsComment comment) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			comment.setUserId(getApiUserId());
			msg.setData(resourceCircleService.addComment(comment));
		} catch (ProAffairException e) {
			LOG.error("-生活圈-发布评论->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-生活圈-发布评论->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 生活圈评论列表 */
	@ApiVersion(1.0)
	@RequestMapping("comment/list")
	public ResponseMsg commentList(@RequestBody PageBean<CommentSearchRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			try {
				request.getParam().setUserId(getApiUserId());
			} catch (Exception e) {
			}
			msg.setData(resourceCircleService.selectCommentForApiList(request));
		} catch (ProException e) {
			LOG.error("-生活圈-评论列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-生活圈-评论列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
